//
//  TogetherHttpClient.m
//  Together_iOS
//
//  Created by applex on 14-3-1.
//  Copyright (c) 2014年 QIU Shuang. All rights reserved.
//

#import "TogetherHttpClient.h"
#import "Stop.h"

#import "AFNetworking.h"

@implementation TogetherHttpClient

+ (void)fetchPredictedStopsWithRegion:(NSString *)region
                                 type:(NSInteger)type
                               before:(NSArray *)before
                                after:(NSArray *)after
                          excludesIds:(NSArray *)excludes
                               userId:(NSNumber *)userId
                              success:(void(^)(NSArray *stops))success
                              failure:(void(^)(NSError *error))failure {
    NSString *recommendUrl = [[self serverAddress] stringByAppendingString:@"/business/recommends"];

    NSString *beforeIdString = [self idStringFromArray:before];
    NSString *afterIdString = [self idStringFromArray:after];
    NSString *excludeIdString = [self idStringFromArray:excludes];

    
    NSDictionary *parameters = @{@"region": region,
                                 @"type": [NSString stringWithFormat:@"%d", (int)type],
                                 @"before": beforeIdString,
                                 @"after": afterIdString,
                                 @"excludes": excludeIdString,
                                 @"userIds": userId};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:recommendUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([dic[@"status"] isEqualToString:@"OK"]) {
                NSArray *stopDics = dic[@"plans"];
                NSLog(@"get recommends success, response obj count: %d", stopDics.count);

                NSMutableArray *stops = [NSMutableArray array];
                for (NSDictionary *sDic in stopDics) {
                    Stop *s = [Stop objectWithDictionary:sDic];
                    [s save];
                    if (s) {
                        [stops addObject:s];
                    }
                }
                if (success) {
                    success(stops);
                }
            }

        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        if (failure) {
            failure(error);
        }
    }];

}


+ (NSString *)idStringFromArray:(NSArray *)array {
    if (!array.count) {
        return @"";
    }
    NSString *idString = @"";
    for (id a in array) {
        if ([a isKindOfClass:[Stop class]]) {
            idString = [idString stringByAppendingFormat:@"%@, ", ((Stop *)a).business_id];
        } else if ([a isKindOfClass:[NSString class]] || [a isKindOfClass:[NSNumber class]]) {
            idString = [idString stringByAppendingFormat:@"%@, ", a];
        }
    }
    idString = [idString substringToIndex:MAX(idString.length - 2, 0)];
    return idString;
}


+ (NSString *)serverAddress {
    NSString *endPont = [NSString stringWithFormat:@"http://%@/together-service-rest", [[NSUserDefaults standardUserDefaults] stringForKey:@"serverAddress"]];
    return endPont;
}

@end
